let id = 0;
export class Dep{
    constructor(){
        this.subs = [];
        this.id = id++;
    }
    addDep(watcher){
        if (watcher && !this.subs.map(sub=>sub.id).includes(watcher.id)){
            this.subs.push(watcher);
        }
    }
    depend(){
        // Dep 与 Watcher 双向绑定
        const watcher = Dep.target;
        if (watcher){
            watcher.addDep(this);
        }
        // 去重(防止收集同一个watcher)
        // if (watcher && !this.subs.map(sub=>sub.id).includes(watcher.id)){
        //     this.subs.push(Dep.target);
        // }
    }
    notify(){
        this.subs.forEach(watcher=>watcher.update());
    }

}
Dep.target = null;